using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using Campus.Entities;

namespace CampusSite.Models
{ 
    public class TurnoRepository : ITurnoRepository
    {
        CampusSiteContext context;        

        public TurnoRepository()
        {
            context = new CampusSiteContext();
        }
        
        public TurnoRepository(CampusSiteContext context)
        {
            this.context = context;
        }  

        public IQueryable<Turno> All
        {
            get { return context.Turnos; }
        }

        public IQueryable<Turno> AllIncluding(params Expression<Func<Turno, object>>[] includeProperties)
        {
            IQueryable<Turno> query = context.Turnos;
            foreach (var includeProperty in includeProperties) {
                query = query.Include(includeProperty);
            }
            return query;
        }

        public Turno Find(int id)
        {
            return context.Turnos.Find(id);
        }

        public void InsertOrUpdate(Turno turno)
        {
            if (turno.TurnoId == default(int)) {
                // New entity
                context.Turnos.Add(turno);
            } else {
                // Existing entity
                context.Entry(turno).State = EntityState.Modified;
            }
        }

        public void Delete(int id)
        {
            var turno = context.Turnos.Find(id);
            context.Turnos.Remove(turno);
            
        }

        public void RemoveByGrupo(int grupoId)
        {
            foreach (var t in context.Turnos.Where(g => g.GrupoId == grupoId))
            {
                context.Turnos.Remove(t);                
            }
            context.SaveChanges();
        }
         public void Save()
        {
            context.SaveChanges();
        }

        public void Dispose() 
        {
            context.Dispose();
        }
    }

    public interface ITurnoRepository : IDisposable
    {
        IQueryable<Turno> All { get; }
        IQueryable<Turno> AllIncluding(params Expression<Func<Turno, object>>[] includeProperties);
        Turno Find(int id);
        void InsertOrUpdate(Turno turno);
        void Delete(int id);
        void Save();
        void RemoveByGrupo(int grupoId);
    }
}